package demo1;

public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public MySingleList(ListNode head) {
        this.head = head;
    }
    public MySingleList() {
        ;
    }

    private ListNode head;
    //打印方法
    public void display(){
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //在头部插入数据
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        node.next = head;
        head = node;
    }
    //在尾部插入数据
    public void addLast(int data){
        ListNode cur = getLastNode();

        ListNode node = new ListNode(data);
        if(cur == null){
            head = node;
            return;
        }
        cur.next = node;
    }
    //在任意位置插入元素
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size()){
            return false;
        }
        ListNode cur = getIndexNode(index - 1);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
        return true;
    }
//    //查找是否包含关键字key是否在单链表当中
//    public boolean contains(int key){
//        ListNode cur = head;
//        while(cur != null){
//            if(cur.val == key){
//                return true;
//            }
//            cur = cur.next;
//        }
//        if(cur.val == key){
//            return true;
//        }else {
//            return false;
//        }
//    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head != null && head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = getNodeIndexQian(key);
        if(cur == null) {
            System.out.println("未找到你要删除的元素");
            return;
        }
        cur.next = cur.next.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
//        if(head != null && head.val == key){
//            head = head.next;
//        }
        ListNode cur = head;
        while(cur != null){
            cur = getNodeIndexQian(key);
            if(cur == null){
                System.out.println("删除完毕");
                return;
            }
            if(head != null && head.val == key){
                head = head.next;
            }
            cur.next = cur.next.next;
        }
    }
    //一次循环全部删完
    public  void removeAllKey2(int key){
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(prev.next != null){
            if(cur.val/*cur*/ == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = prev.next;//cur
                cur = cur.next;
            }
            if(head.val == key){
                head = head.next;
            }
        }
    }
    //获得指定元素的前一个地址
    private ListNode getNodeIndexQian(int key){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //获取指定位置元素
    private ListNode getIndexNode(int Index){
        ListNode cur = head;
        for (int i = 0; i < Index; i++) {
            cur = cur.next;
        }
        return cur;
    }
    //获取最后一个节点的地址
    private ListNode getLastNode(){
        ListNode cur = head;
        if(head == null) return null;
        while(cur.next != null){
            cur = cur.next;
        }
        return cur;
    }
    //获取链表长度
    public  int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //判断链表中是否有key元素
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //在指定位置插入元素
    public void indexadd(int index,int data){
        ListNode cur = head;
        if(index < 0 || index > size()){
            System.out.println("不合法");
            return;
        }
        if(index == 0){
            addFirst(data);
        }
        if(index == size()){
            addLast(data);
        }
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    //获取倒数第n个节点
    public ListNode getLastKey(int n){
        if(n <= 0 || head == null){
            return null;
        }
        ListNode fase = head;
        ListNode slow = head;
        for (int i = 1; i < n; i++) {
            fase = fase.next;
        }
        while(fase.next != null){
            fase = fase.next;
            slow = slow.next;
        }
        return slow;
    }
    //获取两个链表相交的节点
    public MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA,MySingleList.ListNode headB){
        if(headA == null){
            return null;
        }
        if(headB == null){
            return null;
        }
        MySingleList.ListNode PL = headA;
        MySingleList.ListNode PS = headB;
        int lenA = 0;
        int lenB = 0;
        while(PL != null){
            lenA++;
            PL = PL.next;
        }
        while(PS != null){
            lenB++;
            PS = PS.next;
        }
        PL = headA;
        PS = headB;
        int len = lenA - lenB;
        if(lenB > lenA){
            PL = headB;
            PS = headA;
            len = lenB - lenA;
        }
        for (int i = 0; i < len; i++) {
            PL = PL.next;
        }
        while(PL != PS){
            PL = PL.next;
            PS = PS.next;
        }
        return PS;
    }
    //判断字符是否回文

}
